// CabDLL.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"

#define UPGRADEFILE L"\\__ICONTACTAE_UPGRADE.DAT"

void SetUpgrade(LPCTSTR pszInstallDir)
{
  wchar_t path[MAX_PATH+1];
  _tcscpy(path, pszInstallDir);
  _tcscat(path, UPGRADEFILE);
  fclose(_wfopen(path, L"w+"));
}

void ClearUpgrade(LPCTSTR pszInstallDir)
{
  wchar_t path[MAX_PATH+1];
  _tcscpy(path, pszInstallDir);
  _tcscat(path, UPGRADEFILE);
  DeleteFile(path);
}

bool IsUpgrading(LPCTSTR pszInstallDir)
{
  wchar_t path[MAX_PATH+1];
  _tcscpy(path, pszInstallDir);
  _tcscat(path, UPGRADEFILE);
  WIN32_FIND_DATA fd;
  HANDLE hf = FindFirstFileEx(path, FindExInfoStandard, &fd, FindExSearchNameMatch, NULL, 0);
  if (hf != INVALID_HANDLE_VALUE)
  {
    FindClose(hf);
    return true;
  }
  return false;
}

bool IsRegisteredContactManager(LPCTSTR pszInstallDir)
{
  HKEY hk;
  DWORD disp;
  DWORD maxBytes;
  DWORD type;
  bool ret = false;

  if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Shell\\Rai\\:MSCONTACTS", 0, L"", 0, 0, NULL, &hk, &disp) == ERROR_SUCCESS)
  {
    wchar_t ipath[MAX_PATH+1];
    wchar_t cpath[MAX_PATH+1];

    maxBytes = MAX_PATH * sizeof(wchar_t);
    _tcscpy(ipath, L"\"");
    _tcscat(ipath, pszInstallDir);
    _tcscat(ipath, L"\\iContact.exe");
    _tcscat(ipath, L"\"");

    if (RegQueryValueEx(hk, L"1", 0, &type, (PBYTE) cpath, &maxBytes) == ERROR_SUCCESS)
    {
      if (type == REG_SZ && !_wcsicmp(ipath, cpath))
      {
        ret = true;
      }
    }
    
    RegCloseKey(hk);
  }

  return ret;
}

BOOL APIENTRY DllMain(HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
{
  return TRUE;
}

codeINSTALL_INIT Install_Init(HWND hwndParent, BOOL fFirstCall, BOOL fPreviouslyInstalled, LPCTSTR pszInstallDir)
{
  if (fFirstCall && fPreviouslyInstalled)
  {
    SetUpgrade(pszInstallDir);
  }
  return codeINSTALL_INIT_CONTINUE;
}

codeINSTALL_EXIT Install_Exit(HWND hwndParent, LPCTSTR pszInstallDir, WORD cFailedDirs, WORD cFailedFiles, WORD cFailedRegKeys, WORD cFailedRegVals, WORD cFailedShortcuts)
{
  HKEY hk;
  DWORD disp;
  DWORD maxBytes;
  wchar_t ipath[MAX_PATH+1];
  wchar_t path[MAX_PATH+1];
  _tcscpy(path, pszInstallDir);
  _tcscat(path, L"\\settings.ini");

  WIN32_FIND_DATA fd;
  HANDLE hf = FindFirstFileEx(path, FindExInfoStandard, &fd, FindExSearchNameMatch, NULL, 0);
  BOOL copyOverINI = TRUE;

  if (hf != INVALID_HANDLE_VALUE)
  {
    FindClose(hf);

    if (IsUpgrading(pszInstallDir) || MessageBox(hwndParent, L"An existing settings.ini file was found.  Would you like to keep it and use it for this version? (If you have problems, just re-run the CAB installer and choose NO to this question to use the default settings.ini.)", L"iContact AE Installer", MB_YESNO) == IDYES)
    {
      copyOverINI = FALSE;
    }
    else
    {
      if (!DeleteFile(path))
      {
        MessageBox(hwndParent, L"Unable to delete settings.ini!", L"iContact AE Installer", MB_OK);
      }
    }
  }

  if (copyOverINI)
  {
    wchar_t srcpath[MAX_PATH+1];
    wchar_t destpath[MAX_PATH+1];

    _tcscpy(srcpath, pszInstallDir);
    _tcscpy(destpath, pszInstallDir);
    _tcscat(destpath, L"\\settings.ini");

    if (MessageBox(hwndParent, L"Do you want iContacts AE to use default QVGA (width=240 pixels) or VGA (width=480 pixels) optimized settings?  You can change these settings at any time using the configuration application.  YES=QVGA / NO=VGA", L"iContact AE Installer", MB_YESNO) == IDYES)
    {
      _tcscat(srcpath, L"\\settings_qvga.ini");
    }
    else
    {
      _tcscat(srcpath, L"\\settings_vga.ini");
    }

    if (!CopyFile(srcpath, destpath, FALSE))
    {
      MessageBox(hwndParent, L"Unable to automatically create settings.ini!", L"iContact AE Installer", MB_OK);
    }
  }

  if (!IsRegisteredContactManager(pszInstallDir) && MessageBox(hwndParent, L"Would you like iContact AE to be your default system registered Contacts Manager?  A soft reset may be required for the new setting to take effect.  (This means iContacts AE appears when you select \"contacts\" from most other -- but not all -- applications.)", L"iContact AE Installer", MB_YESNO) == IDYES)
  {
    if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Shell\\Rai\\:MSCONTACTS", 0, L"", 0, 0, NULL, &hk, &disp) == ERROR_SUCCESS)
    {
      maxBytes = MAX_PATH * sizeof(wchar_t);
      _tcscpy(ipath, L"\"");
      _tcscat(ipath, pszInstallDir);
      _tcscat(ipath, L"\\iContact.exe");
      _tcscat(ipath, L"\"");
      if (RegSetValueEx(hk, L"1", 0, REG_SZ, (PBYTE) ipath, maxBytes) != ERROR_SUCCESS)
      {
        MessageBox(hwndParent, L"Unable to register iContact AE as default Contacts Manager -- could not save registry value!", L"iContact AE Installer", MB_OK);
      }
      
      RegCloseKey(hk);
    }
    else
    {
      MessageBox(hwndParent, L"Unable to register iContact AE as default Contacts Manager -- could not open registry key!", L"iContact AE Installer", MB_OK);
    }
  }

  // Set iDialer path
  if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, L"Software\\Apps\\Supware.net", 0, L"", 0, 0, NULL, &hk, &disp) == ERROR_SUCCESS)
  {
    maxBytes = MAX_PATH * sizeof(wchar_t);
    _tcscpy(ipath, pszInstallDir);
    _tcscat(ipath, L"\\iContact.exe");
    RegSetValueEx(hk, L"iContact", 0, REG_SZ, (PBYTE) ipath, maxBytes);
    RegCloseKey(hk);
  }

  // Clear the upgrade folder
  ClearUpgrade(pszInstallDir);

  return codeINSTALL_EXIT_DONE;
} 

codeUNINSTALL_INIT Uninstall_Init(HWND hwndParent, LPCTSTR pszInstallDir)
{
  if (!IsUpgrading(pszInstallDir) && MessageBox(hwndParent, L"Would you like to keep your settings.ini file? (Do this if you want to preserve settings for a future installation.)", L"iContact AE Installer", MB_YESNO) == IDNO)
  {
    wchar_t path[MAX_PATH+1];
    _tcscpy(path, pszInstallDir);
    _tcscat(path, L"\\settings.ini");
    if (!DeleteFile(path))
    {
      MessageBox(hwndParent, L"Unable to delete settings.ini!  This file will have to be deleted manually.", L"iContact AE Installer", MB_OK);
    }
  }

  if (!IsUpgrading(pszInstallDir) && IsRegisteredContactManager(pszInstallDir) && MessageBox(hwndParent, L"iContact AE is currently registered as the default contact manager.  Since you are uninstalling, would you like to register Pocket Outlook as the default contacts manager?  A soft reset may be required for the new setting to take effect.", L"iContact AE Installer", MB_YESNO) == IDYES)
  {
    HKEY hk;
    DWORD disp;
    DWORD maxBytes;
    wchar_t ipath[MAX_PATH+1];

    if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Shell\\Rai\\:MSCONTACTS", 0, L"", 0, 0, NULL, &hk, &disp) == ERROR_SUCCESS)
    {
      maxBytes = MAX_PATH * sizeof(wchar_t);
      _tcscpy(ipath, L":MSPOUTLOOK contacts");
      if (RegSetValueEx(hk, L"1", 0, REG_SZ, (PBYTE) ipath, maxBytes) != ERROR_SUCCESS)
      {
        MessageBox(hwndParent, L"Unable to register Pocket Outlook as default Contacts Manager -- could not save registry value!", L"iContact AE Installer", MB_OK);
      }
      
      RegCloseKey(hk);
    }
    else
    {
      MessageBox(hwndParent, L"Unable to register Pocket Outlook as default Contacts Manager -- could not open registry key!", L"iContact AE Installer", MB_OK);
    }
  }

  return codeUNINSTALL_INIT_CONTINUE;
} 

codeUNINSTALL_EXIT Uninstall_Exit(HWND hwndParent)
{
  return codeUNINSTALL_EXIT_DONE;
}
